<h2>Why is this an issue?</h2>
<p>Concatenating multiple string literals or strings using the <code>+</code> operator creates a new string object for each concatenation. This can
lead to a large number of intermediate string objects and can be inefficient. The <code>StringBuilder</code> class is more efficient than string
concatenation, especially when the operator is repeated over and over as in loops.</p>
<h2>How to fix it</h2>
<p>Replace string concatenation with <code>StringBuilder</code>.</p>
<h3>Code examples</h3>
<h4>Noncompliant code example</h4>
<pre data-diff-id="1" data-diff-type="noncompliant">
string str = "";
for (int i = 0; i &lt; arrayOfStrings.Length ; ++i)
{
  str = str + arrayOfStrings[i];
}
</pre>
<h4>Compliant solution</h4>
<pre data-diff-id="1" data-diff-type="compliant">
StringBuilder bld = new StringBuilder();
for (int i = 0; i &lt; arrayOfStrings.Length; ++i)
{
  bld.Append(arrayOfStrings[i]);
}
string str = bld.ToString();
</pre>
<h2>Resources</h2>
<h3>Documentation</h3>
<ul>
  <li> Microsoft Learn - <a href="https://learn.microsoft.com/en-us/dotnet/api/system.text.stringbuilder">StringBuilder Class</a> </li>
</ul>
<h3>Benchmarks</h3>
<table>
  <colgroup>
    <col style="width: 20%;">
    <col style="width: 20%;">
    <col style="width: 20%;">
    <col style="width: 20%;">
    <col style="width: 20%;">
  </colgroup>
  <thead>
    <tr>
      <th>Method</th>
      <th>Runtime</th>
      <th>Mean</th>
      <th>Standard Deviation</th>
      <th>Allocated</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><p>StringConcatenation</p></td>
      <td><p>.NET 9.0</p></td>
      <td><p>50,530.75 us</p></td>
      <td><p>2,699.189 us</p></td>
      <td><p>586280.70 KB</p></td>
    </tr>
    <tr>
      <td><p>StringBuilder</p></td>
      <td><p>.NET 9.0</p></td>
      <td><p>82.31 us</p></td>
      <td><p>3.387 us</p></td>
      <td><p>243.79 KB</p></td>
    </tr>
    <tr>
      <td><p>StringConcatenation</p></td>
      <td><p>.NET Framework 4.8.1</p></td>
      <td><p>37,453.72 us</p></td>
      <td><p>1,543.051 us</p></td>
      <td><p>586450.38 KB</p></td>
    </tr>
    <tr>
      <td><p>StringBuilder</p></td>
      <td><p>.NET Framework 4.8.1</p></td>
      <td><p>178.32 us</p></td>
      <td><p>6.148 us</p></td>
      <td><p>244.15 KB</p></td>
    </tr>
  </tbody>
</table>
<h4>Glossary</h4>
<ul>
  <li> <a href="https://en.wikipedia.org/wiki/Arithmetic_mean">Mean</a> </li>
  <li> <a href="https://en.wikipedia.org/wiki/Standard_deviation">Standard Deviation</a> </li>
</ul>
<p>The results were generated by running the following snippet with <a href="https://github.com/dotnet/BenchmarkDotNet">BenchmarkDotNet</a>:</p>
<pre>
[Params(10_000)]
public int Iterations;

[Benchmark]
public void StringConcatenation()
{
    string str = "";
    for (int i = 0; i &lt; Iterations; i++)
    {
        str = str + "append";
    }
}

[Benchmark]
public void StringBuilder()
{
    StringBuilder builder = new StringBuilder();
    for (int i = 0; i &lt; Iterations; i++)
    {
        builder.Append("append");
    }
    _ = builder.ToString();
}
</pre>
<p>Hardware Configuration:</p>
<pre>
BenchmarkDotNet v0.14.0, Windows 10 (10.0.19045.5247/22H2/2022Update)
12th Gen Intel Core i7-12800H, 1 CPU, 20 logical and 14 physical cores
  [Host]               : .NET Framework 4.8.1 (4.8.9282.0), X64 RyuJIT VectorSize=256
  .NET 9.0             : .NET 9.0.0 (9.0.24.52809), X64 RyuJIT AVX2
  .NET Framework 4.8.1 : .NET Framework 4.8.1 (4.8.9282.0), X64 RyuJIT VectorSize=256
</pre>

